home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / DIZZY / SRC / DIZZYX.C < prev    next >
C/C++ Source or Header  |  1990-12-29  |  8KB  |  289 lines

  1. /*
  2. >>    Dizzy 1.0
  3. >>
  4. >>    A digital circuit simulator & design program for the X Window System
  5. >>
  6. >>    Copyright 1990 Juri Munkki, all rights reserved
  7. >>
  8. >>    Please read the included file called "DizzyDoc" for information on
  9. >>    what your rights are concerning this product.
  10. >>
  11. >>    This file contains the main program and even handling for the X window system.
  12. >>    Consult DizzyMac.c, if you want the mac versions of the same routines.
  13. >>
  14. */
  15.  
  16. #include "dizzy.h"
  17. #ifndef MACINTOSH
  18. #define XMAIN_PROGRAM    YES
  19. #include <sys/time.h>
  20. #include "X11/cursorfont.h"
  21. #include "xstuff.h"
  22. #include "menu.xbm"
  23. #include "tool.xbm"
  24. #include "misc.xbm"
  25. #include "splash.xbm"
  26.  
  27. XRectangle    XClipR;        /*    Another representation of PortRect.    */
  28.  
  29. /*
  30. >>    This routines determines what to do when a button is clicked
  31. >>    on our window. If the file selection dialog is active, nothing
  32. >>    is done.
  33. */
  34. void    ButtonDownEvent()
  35. {
  36.     Point    MousePoint;
  37.     
  38.     if(MainDisable) return;
  39.     
  40.     DownTrigger=TRUE;
  41.     GetMouseDownPoint(&MousePoint);
  42.     if(PtInRect(MousePoint,&ToolR))
  43.         DoToolClick();
  44.     else
  45.     {    CancelDoubleToolClick();
  46.         if(PtInRect(MousePoint,&MenuR))
  47.             DoMenuClick();
  48.         else
  49.         if(PtInRect(MousePoint,&EditR))
  50.             DoEditClick();
  51.     }
  52. }
  53. /*
  54. >>    This routine is called when a expose is received. It first
  55. >>    combines all available expose events and then updates
  56. >>    our window.
  57. */
  58. void    DoRedisplay(w, ignore, calldata)
  59. Widget                        w;
  60. char                        *ignore;
  61. XmDrawingAreaCallbackStruct *calldata;
  62. {
  63.     int             i;
  64.     Rect            bar;
  65.     Region            temp1,temp2;
  66.     XRectangle        tempr;
  67.     Region            UpRegion;
  68.     
  69.     UpRegion=XCreateRegion();
  70.  
  71.     XtAddExposureToRegion(calldata->event, UpRegion);
  72.  
  73.     while(XCheckTypedWindowEvent(Disp, Wind, Expose, &MyEvent))
  74.     {    XtAddExposureToRegion(&MyEvent, UpRegion);
  75.     }
  76.  
  77.     XSetRegion(Disp, Mode, UpRegion);
  78.  
  79.     /*    Paint gray background.    */
  80.     PenBGray();
  81.     SetRect(&bar,0,0,EditR.left,PortRect.bottom);                        PaintRect(&bar);
  82.     SetRect(&bar,EditR.left,0,EditR.right,EditR.top);                    PaintRect(&bar);
  83.     SetRect(&bar,EditR.left,EditR.bottom,EditR.right,PortRect.bottom);    PaintRect(&bar);
  84.     SetRect(&bar,EditR.right,0,PortRect.right,PortRect.bottom);         PaintRect(&bar);
  85.     PenBlack();
  86.  
  87.     FrameRect(&EditOutline);        /*    Frame edit area outline.        */
  88.     PixCopy(MenuPix,0,0,&MenuR);    /*    Paint menu area.                */
  89.     PixCopy(ToolPix,0,0,&ToolR);    /*    Paint tool area.                */
  90.     InvertRect(&MenuHilite);        /*    Invert menu selection (if any).    */
  91.     UpdateButtonTags();                /*    Show which tools are active.    */
  92.  
  93.     ClipEditArea();                    /*    Just sets the coord origin.        */
  94.     temp1=XCreateRegion();            /*    Leave only edit area in the        */
  95.     temp2=XCreateRegion();            /*    clipping region by doing some    */
  96.     tempr.x=EditR.left;                /*    region arithmetic.                */
  97.     tempr.y=EditR.top;
  98.     tempr.width=EditR.right-EditR.left;
  99.     tempr.height=EditR.bottom-EditR.top;
  100.     XUnionRectWithRegion(&tempr,temp2,temp1);
  101.     XIntersectRegion(UpRegion,temp1,temp2);
  102.     XSetRegion(Disp, Mode, temp2);
  103.  
  104.     UpdateSim();                    /*    Update simulation contents.        */
  105.     
  106.     RestoreClipping();                /*    Restore coordinate origin.        */
  107.     XSetRegion(Disp,Mode,UpRegion);    /*    Restore to expose clipping.        */
  108.  
  109.     if(SplashVisible)                /*    Display copyright, if necessary.*/
  110.     {    Rect    SplashBitRect,SplashRect;
  111.         
  112.         SplashBitRect.left=(EditR.left+EditR.right-splash_width)/2;
  113.         SplashBitRect.top=(EditR.top+EditR.bottom-splash_height)/2;
  114.         SplashBitRect.right=SplashBitRect.left+splash_width;
  115.         SplashBitRect.bottom=SplashBitRect.top+splash_height;
  116.         SplashRect=SplashBitRect;
  117.         InsetRect(&SplashRect,-31,-31);
  118.         EraseRect(&SplashRect);                    /*    Erase background.    */
  119.         InsetRect(&SplashRect,-1,-1);
  120.         FrameRect(&SplashRect);                    /*    Draw a frame.        */
  121.         PixCopy(SplashPix,0,0,&SplashBitRect);    /*    Splash!                */
  122.         SetTrashRect(&SplashRect);                /*    Prepare to erase.    */
  123.     }
  124.  
  125.     XDestroyRegion(temp1);            /*    Destroy temporary regions.        */
  126.     XDestroyRegion(temp2);
  127.     XDestroyRegion(UpRegion);
  128.  
  129.     /*    Restore clipping to normal.    */
  130.     XSetClipRectangles(Disp,Mode,0,0,&XClipR,1,Unsorted);
  131. }
  132. /*
  133. >>    When a resize is reported, some data structures need to be
  134. >>    recalculated and the screen needs an update.
  135. */
  136. void    DoResize(w, ignore, ignoremetoo)
  137. Widget    w;
  138. char    *ignore;
  139. char    *ignoremetoo;
  140. {
  141.     Dimension    h,v;
  142.  
  143.     if(XtIsRealized(Canvas))
  144.     {    XClearArea(Disp, XtWindow(Canvas), 0,0,0,0, TRUE);
  145.     }
  146.     XtSetArg(Wargs[0], XtNwidth, &h);
  147.     XtSetArg(Wargs[1], XtNheight, &v);
  148.     XtGetValues(Canvas,Wargs,2);
  149.  
  150.     PortRect.left=0;
  151.     PortRect.top=0;
  152.     PortRect.right=h;
  153.     PortRect.bottom=v;
  154.  
  155.     XClipR.x=0;
  156.     XClipR.y=0;
  157.     XClipR.width=PortRect.right;
  158.     XClipR.height=PortRect.bottom;
  159.     XSetClipRectangles(Disp,Mode,0,0,&XClipR,1,Unsorted);
  160.     
  161.     SetRect(&MenuR,4,4,menu_width+4,menu_height+4);
  162.     SetRect(&ToolR,4,8+menu_height,tool_width+4,menu_height+8+tool_height);
  163.  
  164.     EditR=PortRect;
  165.     EditR.left=(ToolR.right>MenuR.right) ? ToolR.right : MenuR.right;
  166.     InsetRect(&EditR,5,5);
  167.     EditOutline=EditR;
  168.     InsetRect(&EditOutline,-1,-1);
  169. }
  170. /*
  171. >>    This initializes X, Xt and Motif dependent stuff.
  172. >>    Called only once from main.
  173. */
  174. void    UISetup(argc,argv)
  175. int     argc;
  176. char    *argv[];
  177. {
  178.     int         i;
  179.  
  180.     Toplevel=XtInitialize(argv[0],"X Dizzy",NULL,0,&argc,argv);
  181.  
  182.     Disp=XtDisplay(Toplevel);
  183.  
  184.     Black=BlackPixel(Disp,DefaultScreen(Disp));
  185.     White=WhitePixel(Disp,DefaultScreen(Disp));
  186.  
  187.     XtSetArg(Wargs[0], XtNwidth, INITIAL_WIDTH);
  188.     XtSetArg(Wargs[1], XtNheight, INITIAL_HEIGHT);
  189.     XtSetArg(Wargs[2], XtNbackground, White);
  190.     XtSetArg(Wargs[3], XtNforeground, Black);
  191.     
  192.     Canvas = XtCreateManagedWidget("Canvas",xmDrawingAreaWidgetClass,
  193.                                     Toplevel, Wargs, 4);
  194.  
  195.     Wind=XtWindow(Canvas);
  196.     Disp=XtDisplay(Canvas);
  197.  
  198.     XtAddCallback(Canvas, XmNexposeCallback, DoRedisplay, 0L);
  199.     XtAddCallback(Canvas, XmNresizeCallback, DoResize, 0L);
  200.     XtAddEventHandler(Canvas, ButtonPressMask, FALSE, ButtonDownEvent, &Canvas);
  201.  
  202.     Mode=XCreateGC(XtDisplay(Canvas),DefaultRootWindow(XtDisplay(Canvas)),NULL,NULL);
  203.  
  204.     MyFont=XLoadQueryFont(Disp,"-*-helvetica-medium-r-*--10-*");
  205.     XSetFont(Disp,Mode,MyFont->fid);
  206.  
  207.     XSetFunction(Disp,Mode,GXcopy);
  208.     XSetForeground(Disp,Mode,Black);
  209.     XSetBackground(Disp,Mode,White);
  210.  
  211.     XtRealizeWidget(Toplevel);
  212.     MenuPix=CreatePixMap(menu_bits,menu_width,menu_height);
  213.     ToolPix=CreatePixMap(tool_bits,tool_width,tool_height);
  214.     MiscPix=CreatePixMap(misc_bits,misc_width,misc_height);
  215.     SplashPix=CreatePixMap(splash_bits,splash_width,splash_height);
  216.     SplashVisible= -1;
  217.  
  218.     InitXGraf();        /*    More graphics stuff preparation.    */
  219.  
  220.     DoResize(Canvas,0,0);
  221.  
  222. /*    Preset tool palette functions to defaults:                    */
  223.     InputSelector=2;
  224.     InputFrame.left=ToolR.left+34;
  225.     InputFrame.right=InputFrame.left+21;
  226.     InputFrame.top=ToolR.top+50;
  227.     InputFrame.bottom=InputFrame.top+21;
  228.     
  229.     for(i=0;i<MOUSE_BUTTONS;i++)
  230.     {    ToolButtons[i].Function=17+i;
  231.         SetRect(&ToolButtons[i].Prime,
  232.                     ToolR.left+33+i*24,ToolR.bottom-26,
  233.                     ToolR.left+56+i*24,ToolR.bottom-3);
  234.         ToolLocks[i]=ToolButtons[i];
  235.     }
  236.     XGrabButton(Disp, AnyButton, AnyModifier, Wind, TRUE,
  237.                 ButtonPressMask | ButtonMotionMask | ButtonReleaseMask,
  238.                 GrabModeAsync, GrabModeAsync,
  239.                 Wind,XCreateFontCursor(XtDisplay(Canvas),XC_left_ptr));
  240.  
  241. }
  242. /*
  243. >>    What can I say? A main is a main. It contains a main even loop
  244. >>    and calls a few initialization routines.
  245. */
  246. int     main(argc,argv)
  247. int     argc;
  248. char    *argv[];
  249. {
  250.     QuitNow=0;            /*    Do not quite immediatelly.        */
  251.     MainDisable=0;        /*    Do not disable main window.        */
  252.     SimSetup();         /*    Start up simulator variables.    */
  253.     UISetup(argc,argv); /*    Start up user interface stuff.    */
  254.     FileIsNamed=0;        /*    File is now unnamed.            */
  255.     CurFileName[0]=0;    /*    Zero length string.                 */
  256.  
  257.  
  258.     do
  259.     {    if(XtPending())
  260.         {    XtNextEvent(&MyEvent);
  261.             XtDispatchEvent(&MyEvent);
  262.         }
  263.         else
  264.         {    struct timeval    timeout;
  265.             int             readfds=0;
  266.             int             maxfds=1+ConnectionNumber(Disp);
  267.             
  268.             SimLevel=0;
  269.             SimTimer++;
  270.             ClipEditArea();
  271.             RunSimulation();
  272.             RestoreClipping();
  273.             XFlush(Disp);
  274.  
  275.             timeout.tv_sec=0;
  276.             timeout.tv_usec=100000;
  277.             
  278.             readfds= 1 << ConnectionNumber(Disp);
  279.             select(maxfds, &readfds, NULL, NULL, &timeout);
  280.         }
  281.     } while(!QuitNow);
  282.  
  283.     if(QuitNow)
  284.     {    XtCloseDisplay(Disp);
  285.         exit(0);
  286.     }
  287. }
  288. #endif
  289.